package com.sino.config.server.service.config;

import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.lang.ContinueLoop;
import org.nutz.lang.Each;
import org.nutz.lang.ExitLoop;
import org.nutz.lang.Lang;
import org.nutz.lang.LoopException;
import org.nutz.lang.util.NutMap;
import org.nutz.plugin.spring.boot.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sino.config.server.bean.config.Property;
import com.sino.config.server.bean.project.Label;
import com.sino.config.server.bean.project.Profile;
import com.sino.config.server.service.project.ApplicationService;
import com.sino.config.server.service.project.LabelService;
import com.sino.config.server.service.project.ProfileService;

/**
 * @author kerbores
 *
 */
@Service
public class PropertyService extends BaseService<Property> {

	@Autowired
	ProfileService profileService;

	@Autowired
	LabelService labelService;

	@Autowired
	ApplicationService applicationService;

	public NutMap show(String application, String profile, String label) {
		return show(applicationService.fetch(application).getId(), profileService.fetch(profile), labelService.fetch(label));
	}

	public NutMap show(long applicationId, long profileId, long labelId) {
		return show(applicationId, profileService.fetch(profileId), labelService.fetch(labelId));
	}

	public NutMap show(long applicationId, Profile profile, Label label) {

		List<Profile> profiles = profileService.query(Cnd.where("weight", "<=", profile.getWeight()).asc("weight"));// 权重比这个小的全部

		List<Label> labels = labelService.query(Cnd.where("weight", "<=", label.getWeight()).asc("weight"));

		NutMap map = NutMap.NEW();
		Lang.each(profiles, new Each<Profile>() {

			@Override
			public void invoke(int index, Profile p, int length) throws ExitLoop, ContinueLoop, LoopException {
				Lang.each(labels, new Each<Label>() {

					@Override
					public void invoke(int index, Label l, int length) throws ExitLoop, ContinueLoop, LoopException {
						Lang.each(query(Cnd.where("applicationId", "=", applicationId).and("profileId", "=", p.getId()).and("labelId", "=", l.getId()).asc("key")),
								new Each<Property>() {

									@Override
									public void invoke(int index, Property property, int length) throws ExitLoop, ContinueLoop, LoopException {
										map.put(property.getKey(), property.getValue());
									}
								});
					}
				});
			}
		});
		// 合并默认配置
		Lang.each(profiles, new Each<Profile>() {

			@Override
			public void invoke(int index, Profile p, int length) throws ExitLoop, ContinueLoop, LoopException {
				Lang.each(labels, new Each<Label>() {

					@Override
					public void invoke(int index, Label l, int length) throws ExitLoop, ContinueLoop, LoopException {
						Lang.each(query(Cnd.where("applicationId", "=", 0).and("profileId", "=", p.getId()).and("labelId", "=", l.getId()).asc("key")),
								new Each<Property>() {

									@Override
									public void invoke(int index, Property property, int length) throws ExitLoop, ContinueLoop, LoopException {
										map.put(property.getKey(), property.getValue());
									}
								});
					}
				});
			}
		});
		return map;
	}

}
